package com.blb.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.blb.entity.Menu;
import com.blb.entity.MenuExample;
import com.blb.entity.Role;
import com.blb.entity.RoleExample;
import com.blb.entity.RoleExample.Criteria;
import com.blb.mapper.MenuMapper;
import com.blb.mapper.RoleMapper;
import com.blb.utils.MybatisUtils;

public class RoleService {
	/**
	 * 根据ID查询用户
	 * @param id
	 * @return
	 * @throws IOException 
	 */
	public Role getRoleByRoleid(String id) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		RoleMapper roleMapper = session.getMapper(RoleMapper.class);
		RoleExample roleExample = new RoleExample();
		Criteria createCriteria = roleExample.createCriteria();
		createCriteria.andIdEqualTo(id);
		Role role = null;
		List<Role> list = roleMapper.selectByExample(roleExample);
		if (list.size() > 0) {
			role = list.get(0);
		}
		MybatisUtils.closeSession(session);
		return role;
		
	}
	/**
	 * 
	 * 修改用户
	 * @param role
	 * @throws IOException 
	 */
	public void updateRole(Role role) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		RoleMapper roleMapper = session.getMapper(RoleMapper.class);
		RoleExample roleExample = new RoleExample();
		Criteria createCriteria = roleExample.createCriteria();
		createCriteria.andIdEqualTo(role.getId());
		roleMapper.updateByExample(role, roleExample);
	}

	public void delete(List<String> ids) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		RoleMapper roleMapper = session.getMapper(RoleMapper.class);
		
		RoleExample roleExample = new RoleExample();
		Criteria createCriteria = roleExample.createCriteria();
		createCriteria.andIdIn(ids);
		roleMapper.deleteByExample(roleExample);
		session.commit();
		MybatisUtils.closeSession(session);
	}
		
	
	/**
	 * 
	 * 用户添加
	 * @param role
	 * @throws IOException 
	 */
	public void add(Role role) throws IOException {
		SqlSession session = MybatisUtils.getSession();
		RoleMapper roleMapper = session.getMapper(RoleMapper.class);
		roleMapper.insert(role);
		session.commit();
		session.close();
		MybatisUtils.closeSession(session);
	}
	/**
	 * 
	 * 根据角色名查询角色
	 * @param rolename
	 * @throws IOException 
	 */
	public Role selectByrolename(String rolename) throws IOException {
		
		SqlSession session = MybatisUtils.getSession();
		RoleMapper roleMapper = session.getMapper(RoleMapper.class);
		RoleExample roleExample = new RoleExample();
		Criteria createCriteria = roleExample.createCriteria();
		createCriteria.andRolenameEqualTo(rolename);
		Role role = null;
		List<Role> list = roleMapper.selectByExample(roleExample);
		if (list.size() > 0) {
			role = list.get(0);
		}
		MybatisUtils.closeSession(session);
		return role;
		
	}
	/**
	 * 获取父级菜单
	 * @return
	 */
		public List<Map<String, Object>> getParentMenu() throws IOException{
			SqlSession session = MybatisUtils.getSession();
			MenuMapper menuMapper = session.getMapper(MenuMapper.class);
			
			MenuExample menuExample = new MenuExample();
			menuExample.createCriteria().andPidEqualTo("0");
			
			//查询所有目录层次的菜单
			List<Menu> list = menuMapper.selectByExample(menuExample);
			
			//menus就是下拉框需要的数据格式
			List<Map<String, Object>> menus = new ArrayList<Map<String, Object>>();
			
			for (Menu m : list) {
				HashMap<String,Object> map = new HashMap<String,Object>();
				
				map.put("name", m.getMenuname());
				map.put("value",m.getId());
				
				//根据当前菜单id查询该菜单的子菜单
				MenuExample menuExample2 = new MenuExample();
				menuExample2.createCriteria().andPidEqualTo(m.getId());
				List<Menu> list2 = menuMapper.selectByExample(menuExample2);
				//将子菜单转化格式
				List<Map<String, Object>> childrens = new ArrayList<Map<String, Object>>();
				for (Menu mm : list2) {
					HashMap<String,Object> map2 = new HashMap<String,Object>();
					map2.put("name", mm.getMenuname());
					map2.put("value",mm.getId());
					
					List<Map<String, Object>> son = new ArrayList<Map<String, Object>>();
					
					//根据当前菜单id查询该菜单的子菜单
					MenuExample menuExample3 = new MenuExample();
					menuExample3.createCriteria().andPidEqualTo(mm.getId());
					List<Menu> list3 = menuMapper.selectByExample(menuExample3);
					for (Menu mmm : list3) {
						HashMap<String,Object> map3 = new HashMap<String,Object>();
						
						map3.put("name", mmm.getMenuname());
						map3.put("value",mmm.getId());
						son.add(map3);
					}
					map2.put("children", son);
					childrens.add(map2);
				}
				map.put("children", childrens);
				menus.add(map);
			}
			return menus;
		}

	}


